package com.intel.task;

import com.intel.mapper.MedicineMapper;
import com.intel.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created by hduprince on 2017/12/1.
 */
@Slf4j
@Component
public class UploadTask {

    private static Properties properties = new Properties();

    @Resource
    private UploadService uploadService;

    @Resource
    private MedicineMapper medicineMapper;


    /**
     * 查询新增药品上传 测试 3分钟运行一次
     */
    @Scheduled(cron = "0 0/3 * * * ?")
    public void upload(){
        log.info("----------定时任务运行开始----------" + new Date());

        //获取需要上传药品的机构名称
        List<String> orgCodes = medicineMapper.getOrgCodes();

        //载入配置文件
        try {
            properties.load(UploadTask.class.getClassLoader().getResourceAsStream("application-prod.properties"));
        } catch(IOException e) {
            e.printStackTrace();
        }
        orgCodes.forEach(orgCode -> {
            //获取token
            String token = properties.getProperty(orgCode);
            if(token != null) {
                //上传数据
                uploadService.uploadOneByOne(orgCode, token);
                uploadService.executeFeedBackStatus(orgCode);
            } else {
                log.error("药店" + orgCode + "没有token");
            }
        });
        log.info("----------定时任务运行结束----------" + new Date());
    }
}
